package service.impl;

import common.impl.exception.ServiceException;

import model.MTranslate;
import net.sf.json.JSONObject;
import service.interf.ITranslate;
import dao.interf.ITranslateDao;
import dao.pojo.Translate;



public class TranslateImpl implements ITranslate{
	private ITranslateDao translateDao;

	public void settranslateDao(ITranslateDao translateDao) {
		this.translateDao = translateDao;
	}

	@Override
	public String add(MTranslate model) {
		Translate translate = new Translate();

		if(null != model.getTranslateTitle())
			translate.setTranslateTitle(model.getTranslateTitle());
		if(null != model.getTranslateDirection())
			translate.setTranslateDirection(model.getTranslateDirection());
		if(null != model.getTranslateKey())
			translate.setTranslateKey(model.getTranslateKey());

		return translateDao.add(translate);
	}

	@Override
	public String getById(MTranslate model) {
		Translate translate = translateDao.getById(model.getTranslateId());
		if(null == translate) {
			throw new ServiceException("ERR-0151");
		}
		return createTranslateObj(translate).toString();
	}

	@Override
	public String deleteById(Integer id) {
		translateDao.deleteById(id);
		return "1";
	}

	@Override
	public String edit(MTranslate model) {
		Translate translate = translateDao.getById(model.getTranslateId());
		if (null == translate) {
			throw new ServiceException("ERR-0151");
		}

		translate.setTranslateTitle(model.getTranslateTitle());
		translate.setTranslateDirection(model.getTranslateDirection());
		translate.setTranslateKey(model.getTranslateKey());

		translateDao.edit(translate);
		return "1";
	}

	public JSONObject createTranslateObj(Translate translate) {
		JSONObject obj = new JSONObject();

		obj.put("translateId", translate.getTranslateId());	
		obj.put("translateTitle", translate.getTranslateTitle());
		obj.put("translateDirection", translate.getTranslateDirection());
		obj.put("translateKey", translate.getTranslateKey());

		return obj;
	}

}
